﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace Dropfeupload
{
    public class FeuploadFile
    {
        private readonly static string URIRemove = "http://feupload.fe.up.pt/downloads/remove/";
        private readonly static string URIRetrieve = "http://feupload.fe.up.pt/downloads/retrieve/";
        public int fileId;
        public string fileName;
        public string fileFeuploadURI;
        public int downloadCount;

        public void removeFile(FeuploadAuthenticity fa)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(URIRemove + fileId);
                request.CookieContainer = fa.cookies;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                using (var requestStream = request.GetRequestStream())
                {
                    using (var writer = new StreamWriter(requestStream))
                    {
                        writer.Write("_method=post" +
                                     "&authenticity_token=" + fa.authenticity_token);
                    }
                }

                // Needed so the server processes the request
                try {
                    var responseStream = request.GetResponse().GetResponseStream();
                }
                catch
                { }
            }
            catch (Exception e)
            {
            }
        }

        public void downloadFile(String folderDest, FeuploadAuthenticity fa)
        {
            try
            {
                var request = (HttpWebRequest)WebRequest.Create(URIRetrieve);
                request.CookieContainer = fa.cookies;
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                using (var requestStream = request.GetRequestStream())
                {
                    using (var writer = new StreamWriter(requestStream))
                    {
                        writer.Write("authenticity_token=" +
                                       fa.authenticity_token +
                                       "&code=" + fileFeuploadURI +
                                       "&commit=download");
                    }
                }

                using (var responseStream = request.GetResponse().GetResponseStream())
                {
                    int count = 0;
                    byte[] read = new byte[1024];
                    FileStream fs = new FileStream(fileName, FileMode.Create);
                    do
                    {
                        count = responseStream.Read(read, 0, read.Length);
                        fs.Write(read, 0, count);
                    } while (count > 0);
                    fs.Close();
                }
            }
            catch (Exception e)
            {
            }
        }
    }
}
